//
//  BSBModelView.m
//  Baisebudejie
//
//  Created by apple on 16/3/18.
//  Copyright © 2016年 youminuo. All rights reserved.
//

#import "BSBModelView.h"
#import "BSBPubButton.h"

@interface BSBModelView ()

@property (strong, nonatomic) NSArray *imgArray;
@property (strong, nonatomic) NSArray *textArray;

@end

@implementation BSBModelView

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = BSBBackgoundColor;
    [self addButton];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self addanimation];
}

- (void)addButton
{
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    CGFloat btnW = 85;
    CGFloat btnH = 100;
    CGFloat begin = 20;
    CGFloat maginY = 20;
    CGFloat maginX = (self.view.width - 2 * begin - 3 * btnW) / 2;
//    添加按钮
    for (int i = 0 ; i < 6; ++i)
    {
        CGFloat row = i / 3;
        CGFloat col = i % 3;
        
        btnX = begin + col * (btnW + maginX);
        btnY = self.view.height * 0.35 + row * (btnH + maginY);
//        创建按钮
        BSBPubButton *btn = [BSBPubButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
//      设置按钮图片
        UIImage *btnimage = [UIImage imageNamed:self.imgArray[i]];
        [btn setImage:btnimage forState:UIControlStateNormal];
//    设置按钮文字
        [btn setTitle:self.textArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0];
        btn.transform = CGAffineTransformMakeTranslation(0, self.view.height);
        [self.view addSubview:btn];
    }
 
//    设置图片
    UIImageView *titImage = [[UIImageView alloc] init];
    titImage.image = [UIImage imageNamed:@"app_slogan"];
    [titImage sizeToFit];
    titImage.center = CGPointMake(self.view.center.x, self.view.center.y - 170);
    [self.view addSubview:titImage];
    
//    设置底部按钮
    CGFloat maginButton = 10;
    CGFloat buttonH = 40;
    CGFloat buttonY = self.view.height - buttonH - maginButton;
    CGFloat buttonW = self.view.width - 2 * maginButton;
    
//    创建图片
    UIImage *nolimage = [self scalImage:[UIImage imageNamed:@"FollowBtnBg"]];
    UIImage *heightimage = [self scalImage:[UIImage imageNamed:@"FollowBtnClickBg"]];
    
//    创建按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
//    设置按钮背景图片
    [button setBackgroundImage:nolimage forState:UIControlStateNormal];
    [button setBackgroundImage:heightimage forState:UIControlStateHighlighted];
    
//    设置按钮frame
    button.frame = CGRectMake(maginButton, buttonY, buttonW, buttonH);
    
//    设置按钮文字
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
//    监听按钮
    [button addTarget:self action:@selector(missView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

#pragma mark - dismiss方法
- (void)missView
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 给按钮设置动画
- (void)addanimation
{
//    遍历自控件
    for (int i = 0; i < self.view.subviews.count; ++i)
    {
        UIView *btn = self.view.subviews[i];
//        找到要设置动画的按钮
        if ([btn isKindOfClass:[BSBPubButton class]])
        {
//            设置动画
            [UIView animateWithDuration:0.6 + i *0.2 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                btn.transform = CGAffineTransformIdentity;
            } completion:nil];
        }
    }
}

// 缩放图片方法
- (UIImage *)scalImage:(UIImage *)image
{
    UIImage *newimage = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
    return newimage;
}

/**
 *  图片数组懒加载
 */
- (NSArray *)imgArray
{
    if (_imgArray == nil)
    {
        _imgArray = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
    }
    return _imgArray;
}

/**
 *  文字数组懒加载
 */
- (NSArray *)textArray
{
    if (_textArray == nil)
    {
        _textArray = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"];
    }
    return _textArray;
}

@end
